﻿using System;
using System.Data;
using System.Collections.Generic;
using PPWMS.DAL;
using PPWMS.Model;
using PPWMS.Log;
namespace PPWMS.BLL
{
    /// <summary>
    /// Subpack
    /// </summary>
    public partial class Subpack
    {
        private readonly PPWMS.DAL.Subpack dal = new PPWMS.DAL.Subpack();
        private Log.Logmanagement log = new Logmanagement();
        public Subpack() { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string SubPackId)
        {
            bool re = false;
            try
            {
                re = dal.Exists(SubPackId);
            }
            catch (Exception e)
            {
                log.WriteLog(e.Message.ToString());
            }
            return re;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(PPWMS.Model.SubpackModel model)
        {
            bool re = false;
            try
            {
                re = dal.Add(model);
            }
            catch (Exception e)
            {
                log.WriteLog(e.Message.ToString());
            }
            return re;
        }

        public string GetMaxSubpackId(string orderSubId)
        {
            string re = "00";
            try
            {
                re = dal.GetMaxSubpackId(orderSubId);
            }
            catch (Exception e)
            {
                log.WriteLog(e.Message.ToString());
            }
            return re;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PPWMS.Model.SubpackModel model)
        {
            bool re = false;
            try
            {
                re = dal.Update(model);
            }
            catch (Exception e)
            {
                log.WriteLog(e.Message.ToString());
            }
            return re;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string SubPackId)
        {
            bool re = false;
            try
            {
                re = dal.Delete(SubPackId);
            }
            catch (Exception e)
            {
                log.WriteLog(e.Message.ToString());
            }
            return re;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string SubPackIdlist)
        {
            bool re = false;
            try
            {
                re = dal.DeleteList(SubPackIdlist);
            }
            catch (Exception e)
            {
                log.WriteLog(e.Message.ToString());
            }
            return re;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PPWMS.Model.SubpackModel GetModel(string SubPackId)
        {
            SubpackModel model = null;
            try
            {
                model = dal.GetModel(SubPackId);
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public PPWMS.Model.SubpackModel GetModelByCache(string SubPackId)
        {

            string CacheKey = "SubpackModel-" + SubPackId;
            object objModel = PPWMS.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(SubPackId);
                    if (objModel != null)
                    {
                        int ModelCache = PPWMS.Common.ConfigHelper.GetConfigInt("ModelCache");
                        PPWMS.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch (Exception e)
                {
                    log.WriteLog(e.ToString());
                }
            }
            return (PPWMS.Model.SubpackModel)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            DataSet ds = null;
            try
            {
                ds = dal.GetList(strWhere);
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(SubpackModel Order)
        {
            try
            {
                return dal.GetList(Order);
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.Message.ToString());
                return null;
            }
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            DataSet ds = null;
            try
            {
                ds = dal.GetList(Top, strWhere, filedOrder);
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PPWMS.Model.SubpackModel> GetModelList(string strWhere)
        {
            List<PPWMS.Model.SubpackModel> subpackModel = null;
            try
            {
                DataSet ds = dal.GetList(strWhere);
                subpackModel = DataTableToList(ds.Tables[0]);
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return subpackModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PPWMS.Model.SubpackModel> DataTableToList(DataTable dt)
        {
            List<PPWMS.Model.SubpackModel> modelList = new List<PPWMS.Model.SubpackModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                try
                {
                    PPWMS.Model.SubpackModel model;
                    for (int n = 0; n < rowsCount; n++)
                    {
                        model = dal.DataRowToModel(dt.Rows[n]);
                        if (model != null)
                        {
                            modelList.Add(model);
                        }
                    }
                }
                catch (Exception e)
                {
                    log.WriteLog(e.ToString());
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            DataSet ds = null;
            try
            {
                ds = dal.GetList("");
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            int re = -1;
            try
            {
                re = dal.GetRecordCount(strWhere);
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return re;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            DataSet ds = null;
            try
            {
                ds = dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
            }
            catch (Exception e)
            {
                log.WriteLog(e.ToString());
            }
            return ds;
        }
        #endregion  BasicMethod

    }
}

